/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tr.com.argela.tap.mot.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import tr.com.argela.tap.mot.dao.AuthenticationDao;
import tr.com.argela.tap.mot.dao.DbManager;
import tr.com.argela.tap.mot.dao.ModuleDao;
import tr.com.argela.tap.mot.dao.TestPointDao;
import tr.com.argela.tap.mot.logger.LoggerFactory;
import tr.com.argela.tap.mot.models.Module;
import tr.com.argela.tap.mot.models.TestPoint;
import tr.com.argela.tap.mot.models.User;

/**
 * @author arge
 */
public class TestPointServlet extends HttpServlet {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    private static Logger logger = LoggerFactory.getLogger("servlets");

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * 
     * @param request
     *            servlet request
     * @param response
     *            servlet response
     * @throws ServletException
     *             if a servlet-specific error occurs
     * @throws IOException
     *             if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getParameter("point").equals("createpoint")) {

            createpoint(request, response);
        } else if (request.getParameter("point").equals("updatepoint")) {
            updatepoint(request, response);
        } else if (request.getParameter("point").equals("createpoint2")) {
            createpoint2(request, response);
        } else if (request.getParameter("point").equals("updatepoint2")) {
            updatepoint2(request, response);
        }
    }

    public void createpoint(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        logger.info("createpoint called");

        DbManager dbManager = new DbManager();

        try {
            String moduleId = request.getParameter("modulename");
            String testPointName = request.getParameter("pointname");
            String pointDescription = request.getParameter("pdescription");
            String userId = request.getParameter("username");

            if (moduleId != null && testPointName != null && pointDescription != null && userId != null) {
                TestPointDao testPointDao = new TestPointDao();
                System.out.print("Point details" + moduleId + testPointName + pointDescription + userId);
                User user = new User();
                user.setId(Long.parseLong(userId));
                Module module = new Module();
                module.setId(Long.parseLong(moduleId));
                int result = testPointDao.addTestPoint(dbManager, testPointName, module, user, pointDescription);
                TestPoint testpoint = null;
                if (result > 0) {
                    testpoint = new TestPoint();
                    testpoint.setName(testPointName);
                    AuthenticationDao autDao = new AuthenticationDao();
                    user = autDao.getUserById(user.getId());
                    testpoint.setUser(user);
                    ModuleDao moduleDao = new ModuleDao();
                    module = moduleDao.getModuleById(module.getId());
                    testpoint.setModule(module);
                    testpoint.setDescription(pointDescription);
                }
                request.getSession().setAttribute("point", testpoint);
            }
            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }
        response.sendRedirect("newpointproperties.jsp");

    }

    public void createpoint2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        logger.info("create point called");

        DbManager dbManager = new DbManager();

        try {
            String moduleId = request.getParameter("modulename");
            String testPointName = request.getParameter("pointname");
            String pointDescription = request.getParameter("pdescription");
            String userId = request.getParameter("username");

            if (moduleId != null && testPointName != null && pointDescription != null && userId != null) {
                TestPointDao testPointDao = new TestPointDao();
                System.out.print("Point details" + moduleId + testPointName + pointDescription + userId);
                User user = new User();
                user.setId(Long.parseLong(userId));
                Module module = new Module();
                module.setId(Long.parseLong(moduleId));
                int result = testPointDao.addTestPoint(dbManager, testPointName, module, user, pointDescription);
                TestPoint testpoint = null;
                if (result > 0) {
                    testpoint = new TestPoint();
                    testpoint.setName(testPointName);
                    AuthenticationDao autDao = new AuthenticationDao();
                    user = autDao.getUserById(user.getId());
                    testpoint.setUser(user);
                    ModuleDao moduleDao = new ModuleDao();
                    module = moduleDao.getModuleById(module.getId());
                    testpoint.setModule(module);
                    testpoint.setDescription(pointDescription);
                }
                request.getSession().setAttribute("point", testpoint);
            }
            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }
        response.sendRedirect("newpointproperties2.jsp");

    }

    public void updatepoint(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        logger.info("Process request called");

        DbManager dbManager = new DbManager();
        try {

            TestPointDao tpointDao = new TestPointDao();
            Long pointId = Long.parseLong(request.getParameter("pointId"));
            String pointName = request.getParameter("tpointname");
            String pointDescription = request.getParameter("pointdescription");
            String userId = request.getParameter("username");
            User user = new User();
            user.setId(Long.parseLong(userId));
            String moduleId = request.getParameter("mname");
            Module module = new Module();
            module.setId(Long.parseLong(moduleId));

            System.out.print("TestPoint details" + pointId + pointName + moduleId + userId + pointDescription);
            int result = tpointDao.UpdateTpoint(dbManager, pointId, pointName, module, user, pointDescription);

            TestPoint testpoint = null;
            if (result > 0) {
                testpoint = new TestPoint();
                testpoint.setName(pointName);
                AuthenticationDao autDao = new AuthenticationDao();
                user = autDao.getUserById(user.getId());
                testpoint.setUser(user);
                ModuleDao moduleDao = new ModuleDao();
                module = moduleDao.getModuleById(module.getId());
                testpoint.setModule(module);
                testpoint.setDescription(pointDescription);
            }
            request.getSession().setAttribute("point", testpoint);

            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }

        response.sendRedirect("newpointproperties.jsp");
    }

    public void updatepoint2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        logger.info("Process request called");

        DbManager dbManager = new DbManager();
        try {

            TestPointDao tpointDao = new TestPointDao();
            Long pointId = Long.parseLong(request.getParameter("pointId"));
            String pointName = request.getParameter("tpointname");
            String pointDescription = request.getParameter("pointdescription");
            String userId = request.getParameter("username");
            User user = new User();
            user.setId(Long.parseLong(userId));
            String moduleId = request.getParameter("mname");
            Module module = new Module();
            module.setId(Long.parseLong(moduleId));

            System.out.print("TestPoint details" + pointId + pointName + moduleId + userId + pointDescription);
            int result = tpointDao.UpdateTpoint(dbManager, pointId, pointName, module, user, pointDescription);

            TestPoint testpoint = null;
            if (result > 0) {
                testpoint = new TestPoint();
                testpoint.setName(pointName);
                AuthenticationDao autDao = new AuthenticationDao();
                user = autDao.getUserById(user.getId());
                testpoint.setUser(user);
                ModuleDao moduleDao = new ModuleDao();
                module = moduleDao.getModuleById(module.getId());
                testpoint.setModule(module);
                testpoint.setDescription(pointDescription);
            }
            request.getSession().setAttribute("point", testpoint);

            dbManager.commit();
        } catch (Exception e) {
            dbManager.rollback();
        } finally {
            dbManager.close();
        }

        response.sendRedirect("newpointproperties2.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * 
     * @param request
     *            servlet request
     * @param response
     *            servlet response
     * @throws ServletException
     *             if a servlet-specific error occurs
     * @throws IOException
     *             if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * 
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
